package com.ereport.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ereport.entity.Accounts;
import com.ereport.entity.DailyActivity;
import com.ereport.entity.OppParts;
import com.ereport.entity.Opportunity;
import com.ereport.util.ApplicationContext;

public class OpportunityDAO {

static Transaction tx;
	
	private  void endOperations(Session session) {
		
		session.close();
		session=null;
	}

	private  Session startOperations()throws Exception {
		
		Session session = ApplicationContext.getHiberSession().openSession();
		tx=session.beginTransaction();
		return session;
		
	}

	public int insert(Opportunity da) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(da);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
		
	}

	public Long maxid(int id, String eqtype) throws Exception {
		Session session=startOperations();
		String type=eqtype;
		String querystr = null;
		Calendar currentDate = Calendar.getInstance();
		  SimpleDateFormat formatter= 
		  new SimpleDateFormat("yyyy-MM-dd");
		  String dt = formatter.format(currentDate.getTime());
		String[] temp=dt.split("-");
		String year=temp[0];
		String month=temp[1];
		String date=temp[2];
		String dat=null;
		if(eqtype.equals("CP")||eqtype.equals("TE")){
			dat=month+"-"+year;
			querystr="select COUNT(*)+1 from Opportunity where exe_id=:exeid and enq_num like '%"+eqtype+"%"+dat+"%'";
			
		}
		else if(eqtype.equals("BQ")){
			dat=year;
			if(date.equals("01")&&month.equals("04")){
				dat=date+"-"+month+"-"+year;
				querystr="select COUNT(*)+1 from Opportunity where exe_id=:exeid and ref_num like '%BQ%01-04-"+year+"%'";
			}
			else
			{
			querystr="select COUNT(*)+1 from Opportunity where exe_id=:exeid and ref_num like '%BQ%"+year+"%'";
			}
		}
		
		Query query=session.createQuery(querystr);
		query.setInteger("exeid",id);
		Object queryresult=query.uniqueResult();
		Long opp=(Long) queryresult;
		//JOptionPane.showMessageDialog(null,opp);
		session.getTransaction().commit();
		endOperations(session);
		return opp;
	}

	public void deleteOpp(int id, String enq) throws Exception {
		Session session=startOperations();
		String qtr="delete from Opportunity where exe_id=:id and enq_num=:enq";
		Query qry=session.createQuery(qtr);
		qry.setInteger("id", id);	
		qry.setString("enq", enq);	
		qry.executeUpdate();
		endOperations(session);
		return ;
		
	}

	public Opportunity getOppId(int id, String enq) throws Exception {
		Session session=startOperations();
		String qtr="from Opportunity where exe_id=:id and enq_num=:enq";
		Query qry=session.createQuery(qtr);
		qry.setInteger("id", id);	
		qry.setString("enq",enq);	
		Opportunity obj=(Opportunity) qry.uniqueResult();
		endOperations(session);
		return obj;
	}

	public void deleteParts(int id) throws Exception {
		Session session=startOperations();
		String qtr="delete from OppParts where enq_id=:id";
		Query qry=session.createQuery(qtr);
		qry.setInteger("id", id);	
		qry.executeUpdate();
		endOperations(session);
		return ;
		
	}

	public Opportunity getEnqReport(int id, int daid) throws Exception {
		Session session=startOperations();
		String qtr="from Opportunity where exe_id=:id and dact_id=:daid";
		System.out.println("date in dao "+id);
		System.out.println("date in dao "+daid);
		//System.out.println("date in dao "+dat);
		Query qry=session.createQuery(qtr);
		qry.setInteger("id", id);	
		qry.setInteger("daid",daid);	
		//qry.setDate("date",dat);	
		Opportunity obj=(Opportunity) qry.uniqueResult();
		endOperations(session);
		return obj;
	}
	public Opportunity getPreEnqReport(int daid) throws Exception {
		Session session=startOperations();
		String qtr="from Opportunity where dact_id=:daid";
		System.out.println("date in dao "+daid);
		Query qry=session.createQuery(qtr);
		qry.setInteger("daid",daid);	
		Opportunity obj=(Opportunity) qry.uniqueResult();
		endOperations(session);
		return obj;
	}

	public List<OppParts> getParts(int id) throws Exception {
		List<OppParts> ob=new ArrayList<OppParts>();
		Session session = startOperations();
		String qrystr="From OppParts where enq_id=:id";
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", id);
		ob=qr.list();
		endOperations(session);
		return ob;
	}

	public DailyActivity getDAct(int id,int orgid ) throws Exception {
		Session session=startOperations();
		String qtr="from DailyActivity where exe_id=:id and id=:orgid";
		System.out.println("date in dao "+id);
		System.out.println("date in dao "+orgid);
		Query qry=session.createQuery(qtr);
		qry.setInteger("id", id);	
		qry.setInteger("orgid",orgid);	
		DailyActivity obj=(DailyActivity) qry.uniqueResult();
		endOperations(session);
		return obj;
	}

	public List<Opportunity> getAll(int id) throws Exception {
		List<Opportunity> ob=new ArrayList<Opportunity>();
		Session session = startOperations();
		String qrystr="From Opportunity where exe_id=:id";
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", id);
		ob=qr.list();
		endOperations(session);
		return ob;
	}
	public List<Opportunity> getAllEnq() throws Exception {
		List<Opportunity> ob=new ArrayList<Opportunity>();
		Session session = startOperations();
		String qrystr="From Opportunity";
		Query qr=session.createQuery(qrystr);
		ob=qr.list();
		endOperations(session);
		return ob;
	}
}
